package com.eos.control.action.goods;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eos.business.service.SysDeliverReceiptBusinessService;
import com.eos.business.service.SysProductBusinessService;
import com.eos.common.tool.ConstantTool;
import com.eos.common.tool.DataCacheUtil;
import com.eos.common.tool.ObjectUtils;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.ProductBrand;
import com.eos.orm.mybatis.model.SysCustomer;
import com.eos.orm.mybatis.model.SysProduct;
import com.eos.orm.mybatis.model.ViewReceiptList;
import com.hf.utils.PaginationBean;

public class BillOfSignListAction extends AbstractBaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7560110820557163108L;
	private String productId;
	private String customerId;
	private String brandId;
	private String modelId;
	private String barCode;
	private String signTime;
	private Integer pageNum;
	private Integer numPerPage;
	private List<SysCustomer> customerList;
	private List<SysProduct> productList;
	private String brandOfProductJson;
	private Integer totalCount;
	private List<ViewReceiptList> viewReceiptList;

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProductId() {
		return productId;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public String getModelId() {
		return modelId;
	}

	public void setModelId(String modelId) {
		this.modelId = modelId;
	}

	public String getBarCode() {
		return barCode;
	}

	public void setBarCode(String barCode) {
		this.barCode = barCode;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(Integer numPerPage) {
		this.numPerPage = numPerPage;
	}

	public String getBrandOfProductJson() {
		return brandOfProductJson;
	}

	public List<SysCustomer> getCustomerList() {
		return customerList;
	}

	public List<SysProduct> getProductList() {
		return productList;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public String getSignTime() {
		return signTime;
	}

	public void setSignTime(String signTime) {
		this.signTime = signTime;
	}

	public List<ViewReceiptList> getViewReceiptList() {
		return viewReceiptList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() {
		try {
			SysProductBusinessService spbs = new SysProductBusinessService();
			SysProduct product = new SysProduct();
			product.setDealFlag(ConstantTool.DEAL_FALG_OK);
			product.setStatus(ConstantTool.STATUS_OK);
			productList = spbs.getSysProductExample(product);
			ProductBrand brand = new ProductBrand();
			brand.setDealFlag(ConstantTool.DEAL_FALG_OK);
			brand.setStatus(ConstantTool.STATUS_OK);
			List<ProductBrand> brandList = spbs.getProductBrandExample(brand);
			brandOfProductJson = getProductBrandJson(productList, brandList);

			List<SysCustomer> suList = DataCacheUtil.getAllSysCustomer();
			if (null != suList) {
				List<SysCustomer> nsu = new ArrayList<SysCustomer>();
				for (SysCustomer su : suList) {
					if ("0".equals(su.getCustomerType())) {// 0结算客户;1供应商;2融信
						nsu.add(su);
					}
				}
				customerList = nsu;
			}
			if (pageNum == null || pageNum.intValue() == 0)
				pageNum = 1;
			if (numPerPage == null || numPerPage.intValue() == 0)
				numPerPage = PAGE_SIZE;
			SysDeliverReceiptBusinessService sdrs = new SysDeliverReceiptBusinessService();
			Map<String, String> map = new HashMap<String, String>();
			map.put("bar_code", barCode);
			map.put("product_id", productId);
			map.put("brand_id", brandId);
			map.put("model_id", modelId);
			map.put("receipt_time", signTime);
			map.put("customer_id", customerId);
			map.put("cur_page", pageNum + "");
			map.put("page_size", numPerPage + "");
			map.put("table_name", "view_receipt_list");
			PaginationBean pb = sdrs.getReceiptListByView(map);
			totalCount = null != pb ? pb.getMaxcount() : 0;
			viewReceiptList = null != pb ? (List<ViewReceiptList>) pb
					.getResultList() : null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	private String getProductBrandJson(List<SysProduct> proList,
			List<ProductBrand> brandList) {
		if (null != proList && proList.size() > 0 && null != brandList
				&& brandList.size() > 0) {
			Map<String, List<ProductBrand>> map = new HashMap<String, List<ProductBrand>>();
			for (SysProduct pro : proList) {
				for (ProductBrand brand : brandList) {
					int proId = pro.getId();
					if (brand.getProductId() == proId) {
						if (map.containsKey("pro_" + proId)) {
							map.get("pro_" + proId).add(brand);
						} else {
							List<ProductBrand> bList = new ArrayList<ProductBrand>();
							bList.add(brand);
							map.put("pro_" + proId, bList);
						}
					}
				}
			}
			return ObjectUtils.mapToJson(map);
		}
		return "";
	}
}
